/*
 * 文件名：SuperProfitController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：huangxiaolang
 * 修改时间：2020年1月10日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.fia.web;


import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.SuperProfitDao;
import top.infopub.mgr.fia.domain.SuperProfitDomain;
import top.infopub.mgr.fia.service.SuperProfitService;
import top.infopub.mgr.fia.vo.SuperProfitVo;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.model.fia.SuperProfit;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import com.github.pagehelper.Page;


@Controller
@RequestMapping("/superProfit")
public class SuperProfitController {

    @Autowired
    private SuperProfitDao superProfitDao;

    @Autowired
    private SuperProfitService superProfitService;

    @Autowired
    private StaffInfoService staffInfoService;

    /**
     * 建投总部净利润列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "fia/lay-suporProfit-list";
    }

    /**
     * 建投总部净利润列表数据查询
     * @return
     * @throws ParseException 
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<SuperProfitDomain> queryDataByCondition(SuperProfitVo superProfitVo,
                                                          ParamPage paramPage,
                                                          String startBeeDateWithoutDay,
                                                          String endBeeDateWithoutDay)
        throws ParseException {

        Page<SuperProfitDomain> page = new Page<SuperProfitDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (superProfitVo.getEndDate() != null) {
            superProfitVo.setEndDate(DateUtils.addDays(superProfitVo.getEndDate(), 1));
        }
        if (StringUtils.isNotBlank(startBeeDateWithoutDay)) {
            superProfitVo.setStartBeeDate(DateUtils.parseDate(startBeeDateWithoutDay, "yyyy-MM"));
        }
        if (StringUtils.isNotBlank(endBeeDateWithoutDay)) {
            superProfitVo.setEndBeeDate(DateUtils.parseDate(endBeeDateWithoutDay, "yyyy-MM"));
        }
        page = superProfitService.selectPageByDomainWhere(page, superProfitVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 进入增加建投总部净利润数据
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "fia/lay-suporProfit-add";
    }

    /**
     * 建投总部净利润-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(HttpServletRequest request, SuperProfit superProfit, String monthWithoutDay)
        throws ParseException {
        superProfit.setBeeDateMonth(DateUtils.parseDate(monthWithoutDay, "yyyy-MM"));
        superProfitService.saveSpFit(superProfit);
        return RspVo.getSuccessResponse();
    }

    /**
     * 建投总部净利润-作废
     * @throws ParseException 
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public Boolean cancel(HttpServletRequest request, String id) {
        Boolean flag = false;
        SuperProfit superProfit = superProfitService.selectByPrimaryKey(id);
        if (superProfit.getState() == 1) {
            flag = superProfitService.changeState(id, 2);
        }
        if (superProfit.getState() == 2) {
            flag = superProfitService.changeState(id, 1);
        }
        return flag;
    }

    /**
     * 建投总部净利润-详情
     */
    @RequestMapping("/view")
    public ModelAndView details(HttpServletRequest request, String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        SuperProfitDomain superProfit = superProfitService.queryDomainById(id);
        map.put("superProfit", superProfit);
        return new ModelAndView("fia/lay-suporProfit-details", map);
    }

}
